package io.github.harmonly.service.impl;

import io.github.harmonly.entity.User;
import io.github.harmonly.entity.association.*;
import io.github.harmonly.mapper.AssociationMapper;
import io.github.harmonly.mapper.MemberMapper;
import io.github.harmonly.mapper.UserMapper;
import io.github.harmonly.service.AssociationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AssociationServiceImpl implements AssociationService {

    @Resource
    private AssociationMapper mapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private MemberMapper memberMapper;

    /**
     * Association
     */
    @Override
    public List<Association> getAssociations() {
        return mapper.getAssociations();
    }

    @Override
    public List<Association> getAssociationsByName(String name) {
        return mapper.getAssociationsByName(name);
    }

    @Override
    public List<Association> getAssociationsByTag(String tag) {
        return mapper.getAssociationsByTag(tag);
    }

    @Override
    public Association getAssociation(int aid) {
        return mapper.getAssociation(aid);
    }

    @Override
    public boolean addAssociation(Association association) {
        User user = userMapper.getUser(association.getUid());
        user.setRole("master");
        userMapper.editUser(user);
        if (association.getFace().trim().equals("")) association.setFace("/ss/static/images/1.jpeg");
        return mapper.addAssociation(association) == 1;
    }

    @Override
    public boolean deleteAssociation(int aid) {
        Association association = getAssociation(aid);
        if (association != null) {
            User user = userMapper.getUser(association.getUid());
            String role = "member";
            if (memberMapper.getMembersByUser(user.getUid()).size() == 0) role = "student";
            user.setRole(role);
            userMapper.editUser(user);
        }
        return mapper.deleteAssociation(aid) == 1;
    }

    @Override
    public boolean editAssociation(Association association) {
        return mapper.editAssociation(association) == 1;
    }

    /**
     * Association Honor
     */
    @Override
    public List<AssociationHonor> getHonors() {
        return mapper.getHonors();
    }

    @Override
    public List<AssociationHonor> getHonorsByAid(int aid) {
        return mapper.getHonorsByAid(aid);
    }

    @Override
    public AssociationHonor getHonor(int ahid) {
        return mapper.getHonor(ahid);
    }

    @Override
    public boolean addHonor(AssociationHonor honor) {
        return mapper.addHonor(honor) == 1;
    }

    @Override
    public boolean deleteHonor(int ahid) {
        return mapper.deleteHonor(ahid) == 1;
    }

    @Override
    public boolean editHonor(AssociationHonor honor) {
        return mapper.editHonor(honor) == 1;
    }
}
